package com.amos.knife4j.tool;

import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 转换工具类
 *
 * @author meng_lbo
 * @date 2018/12/26  17:31
 */
public class ConvertUtils {
    public ConvertUtils() {
    }

    /**
     * 分页转换
     *
     * @author meng_lbo
     * @date 2018/12/26  15:07
     */
    public <S, T> PageInfo<T> convertPage(PageInfo<S> page, ConvertListCallBack<S, T> callBack) {
        List<T> list = this.convertList((List) page.getList(), callBack);
        PageInfo<T> newPage = new PageInfo<>(list);
        PageInfoUtil.pageInfoTrans(newPage, page);
        return newPage;
    }

    /**
     * 集合类型转换
     *
     * @author meng_lbo
     * @date 2018/12/26  14:34
     */
    public <S, T> List<T> convertList(List<S> list, ConvertListCallBack<S, T> callBack) {
        List<T> targetList = new ArrayList();
        if (callBack != null) {
            list.forEach(n -> {
                T target = callBack.convert(n);
                if (target != null) {
                    targetList.add(target);
                }
            });
        }
        return targetList;
    }


    public interface ConvertListCallBack<S, T> {
        T convert(S var1);
    }
}